
#include <btPointConstraint.h>

#include <BLabel>
#include <BGridLayout>
#include <BVBoxLayout>
#include <BGroupBox>

#include "StringUtils.h"
#include "SliderConstraintPanel.h"

SliderConstraintPanel::SliderConstraintPanel(BWidget* parent) : BWidget(parent)
{
	this->setTitle(BSliderConstraint::TypeName());

	BGroupBox* angularLimitGroupBox = new BGroupBox("Angular Limit");
	{
		BLabel* minimumLabel = new BLabel("minimum");
		_angularMinSpinBox = new BRealSpinBox(-999, 999);

		BLabel* maximumLabel = new BLabel("maximum");
		_angularMaxSpinBox = new BRealSpinBox(-999, 999);

		BGridLayout* gridLayout = new BGridLayout(angularLimitGroupBox);
		gridLayout->setWidget(4, 0, minimumLabel);
		gridLayout->setWidget(4, 1, _angularMinSpinBox);
		gridLayout->setWidget(5, 0, maximumLabel);
		gridLayout->setWidget(5, 1, _angularMaxSpinBox);
	}

	_motorGroupBox = new BGroupBox("Motor");
	_motorGroupBox->setCheckable(true);
	_motorGroupBox->setChecked(true);
	{
		BLabel* targetVelocityLabel = new BLabel("Target Velocity");
		_targetVelocitySpinBox = new BRealSpinBox();

		BLabel* maxImpulseLabel = new BLabel("Max Impulse");
		_maxImpulseSpinBox = new BRealSpinBox();

		BGridLayout* gridLayout = new BGridLayout(_motorGroupBox);
		gridLayout->setWidget(2, 0, targetVelocityLabel);
		gridLayout->setWidget(2, 1, _targetVelocitySpinBox);
		gridLayout->setWidget(3, 0, maxImpulseLabel);
		gridLayout->setWidget(3, 1, _maxImpulseSpinBox);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addWidget(angularLimitGroupBox);
	vlayout->addWidget(_motorGroupBox);

	connect(this, Signal_AssetChanged, &SliderConstraintPanel::slotAssetChanged);

	connect(_angularMinSpinBox, Signal_EditFinished, &SliderConstraintPanel::slotAngularLimitEditFinished);
	connect(_angularMaxSpinBox, Signal_EditFinished, &SliderConstraintPanel::slotAngularLimitEditFinished);
	connect(_motorGroupBox, Signal_Toggled, &SliderConstraintPanel::slotMotorEnabledToggled);
	connect(_targetVelocitySpinBox, Signal_EditFinished, &SliderConstraintPanel::slotTargetVelocityEditFinished);
	connect(_maxImpulseSpinBox, Signal_EditFinished, &SliderConstraintPanel::slotMaxImpulseEditFinished);
}
SliderConstraintPanel::~SliderConstraintPanel()
{

}

void SliderConstraintPanel::slotAssetChanged(BObject* object, const BValue& value)
{
	_sliderConstraint = value;
	if (_sliderConstraint)
	{
		const BRealRange& angleLimit = _sliderConstraint->angleLimit();
		_angularMinSpinBox->setValue(angleLimit.min());
		_angularMaxSpinBox->setValue(angleLimit.max());

	}
}

void SliderConstraintPanel::slotAngularLimitEditFinished(BObject* object, const BValue& value)
{
	BReal min = _angularMinSpinBox->value();
	BReal max = _angularMaxSpinBox->value();
	BRealRange limit = _sliderConstraint->angleLimit();
	limit.set(min, max);
	_sliderConstraint->setAngleLimit(limit);
}
void SliderConstraintPanel::slotMotorEnabledToggled(BObject* object, const BValue& value)
{
	bool motorEnabled = value;

}
void SliderConstraintPanel::slotTargetVelocityEditFinished(BObject* object, const BValue& value)
{
	BReal targetVelocity = value;

}
void SliderConstraintPanel::slotMaxImpulseEditFinished(BObject* object, const BValue& value)
{
	BReal maxImpulse = value;

}
